/**
 * Copyright (C), 2015-2020, XXX有限公司
 * FileName: FilterDemo
 * Author:   ZX
 * Date:     2020/7/27 16:10
 * Description:
 * History:
 * <author>          <time>          <version>          <desc>
 * 作者姓名           修改时间           版本号              描述
 */
package org.example.filter;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import java.io.IOException;
import java.util.Map;

/**
 * 〈一句话功能简述〉<br> 
 * 〈〉
 *
 * @author ZX
 * @create 2020/7/27
 * @since 1.0.0
 */
//@WebFilter(urlPatterns = {"/hello/*"})
public class FilterDemo implements Filter {

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        System.out.println(this.getClass().getName()+"被加载了");

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        Map<String, String[]> parameterMap = servletRequest.getParameterMap();
        parameterMap.entrySet().forEach(entry->{
            if (entry.getKey().equals("name")){
                String[] names = entry.getValue();
                if (names[0].contains("z")){
                    try {
                        filterChain.doFilter(servletRequest,servletResponse);
                    } catch (IOException e) {
                        e.printStackTrace();
                    } catch (ServletException e) {
                        e.printStackTrace();
                    }
                }
            }
        });
        RequestDispatcher rd = servletRequest.getRequestDispatcher("/WEB-INF/filterDemo.jsp");
        rd.forward(servletRequest,servletResponse);

    }

    @Override
    public void destroy() {
        System.out.println(this.getClass().getName()+"被销毁了");

    }
}